package com.ccc3gc.mysystem.service.impl;

import com.ccc3gc.mysystem.VO.UserVO;
import com.ccc3gc.mysystem.bean.User;
import com.ccc3gc.mysystem.mapper.UserMapper;
import com.ccc3gc.mysystem.service.UserService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UserMapper userMapper;
    /**查询用户列表
     * @return users
     */
    @Override
    public List<User> selectUserList(User user) {
        List<User> users = userMapper.selectuser(user);
        return users;
    }

    /**
     * 根据用户Id查询用户详情
     *
     * @param userId
     * @return
     */
    @Override
    public UserVO selectUserById(int userId) {
        UserVO userVO = userMapper.selectUserById(userId);
        return userVO;
    }

    /**
     * @param user
     * @return
     */
    @Override
    public User userLogin(User user) {
        User userLogin = userMapper.selectUserLogin(user);
        return userLogin;
    }

    /**新增用户
     * @param user
     * @return rows
     */
    @Override
    public int instertUser(User user) {
        int rows=userMapper.insterUser(user);
        return rows;
    }

    /**导入
     * @param userList
     * @return
     */
    @Override
    public String importUser(List<User> userList) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (User user : userList)
        {
            try
            {
                user.setPassword("123456");
                userMapper.insterUser(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**修改用户信息
     * @param user
     * @return rows
     */
    @Override
    public int updateUser(User user) {
        int rows = userMapper.updateUser(user);
        return rows;
    }

    /**根据Id删除用户
     * @param userId
     * @return rows
     */
    @Override
    public int deleteUserById(int userId) {
        int rows = userMapper.deleteUserById(userId);
        return rows;
    }

    /**批量删除
     * @param userIds
     * @return rows
     */
    @Override
    public int deleteUserByIds(List<Integer> userIds) {
        int rows = userMapper.deleteUserByIds(userIds);
        return rows;
    }
}
